package conqueror.com.camerarecord;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {


    ImageView ivReord;
    private final String TAG = "Recorder";
    private RecordService recordService = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        bindService(new Intent(this, RecordService.class), conn, Context.BIND_AUTO_CREATE);
        Log.e(TAG, "bindService success");
    }

    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            recordService = ((RecordService.LocalBinder) service).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    private void initView() {
        ivReord = (ImageView) findViewById(R.id.imageViewRecord);
    }

    public void onClickRecord(View v) {
//        recordService.openBigWindow();
        finish();
        stopService(new Intent(MainActivity.this, RecordService.class));
        System.exit(0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //获取焦点重新显示窗口
        if (recordService != null) {
            recordService.mWindowUtil.openWindow(recordService.fl);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        //失去焦点时隐藏窗口
        if (recordService != null) {
            recordService.mWindowUtil.hideFloatWindow(recordService.fl);
        }
    }

    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "unbind service");
        unbindService(conn);
    }

    @Override
    public void finish() {
//		super.finish();
        moveTaskToBack(true);
    }
}
